//
//  ZPRootTableViewController.m
//  zoneSun
//
//  Created by Zeaple on 15/12/24.
//  Copyright © 2015年 Zeaple. All rights reserved.
//

#import "ZPRootTableViewController.h"

@interface ZPRootTableViewController ()

@end

@implementation ZPRootTableViewController

#pragma mark - Propertys
- (UITableView *)tableView {
    if (!_tableView) {
//        CGRect tableViewFrame = self.view.bounds;
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviBarHeight, Main_Screen_Width, Main_Screen_Height-kNaviBarHeight) style:self.tableViewStyle];
        _tableView.backgroundColor = DEF_COLOR242;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _dataSource;
}

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.separatorColor = RGBCOLOR(230, 230, 230);
//    self.tableView.tableFooterView = [[UIView alloc]init];
    UIView *foterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 140)];
    
    UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    logoutButton.frame=CGRectMake(0, 0, 240, 40);
    logoutButton.center = foterView.center;
    logoutButton.backgroundColor = [UIColor blackColor];
    logoutButton.layer.cornerRadius = 20;
    logoutButton.clipsToBounds = YES;
    [logoutButton setTitle:@"退出登录"forState:UIControlStateNormal];
    [logoutButton addTarget:self action:@selector(logOutAction) forControlEvents:UIControlEventTouchUpInside];

    logoutButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [logoutButton setTitleColor:DEF_COLOR forState:UIControlStateNormal];
    [foterView addSubview:logoutButton];
    self.tableView.tableFooterView = foterView;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
}

- (void)logOutAction{
    
    
    
}

#pragma mark - UITableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    SettingGroup *group = self.dataSource[section];
    return group.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 1.创建cell
    MeCenterCell *cell = [MeCenterCell cellWithTableView:tableView];
    
    // 2.给cell传递模型数据
    SettingGroup *group = self.dataSource[indexPath.section];
    cell.item = group.items[indexPath.row];
    
    // 3.返回cell
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.取消选中这行
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 2.模型数据
    SettingGroup *group = self.dataSource[indexPath.section];
    SettingItem *item = group.items[indexPath.row];
    
    // block有值(点击这个cell,.有特定的操作需要执行【控制器跳转除外】)
    if (item.option) {
        item.option();
    } else if ([item isKindOfClass:[IndicatorSettingItem class]]) { // 有箭头的可以跳转控制器
        IndicatorSettingItem *indicatorSettingItem = (IndicatorSettingItem *)item;
        if (indicatorSettingItem.destVcClass == nil) return;
        BaseViewController *vc = [[indicatorSettingItem.destVcClass alloc] init];
        vc.vcName = indicatorSettingItem.vcTitle;
        if(indicatorSettingItem.presentVC){
            BaseNavigationViewController *baseNavi = [[BaseNavigationViewController alloc]initWithRootViewController:vc];
            [self presentViewController:baseNavi animated:YES completion:nil];
            return;
        }
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc  animated:YES];
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    SettingGroup *group = self.self.dataSource[section];
    if(!group.header) return nil;
    return group.header;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    SettingGroup *group = self.dataSource[section];
    if(!group.footer) return nil;
    return group.footer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    CGFloat scale = Main_Screen_Height/3;
    return Main_Screen_Height/scale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
         return 1;
    }else{
        return 12;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
